/**
 * Created by adeline on 2016/10/26.
 */

import Constant from 'common/constant'
import CommonUtil from 'common/common.util'
import RemoteDAO from 'common/remoteDAO'
import LocalDAO from 'common/localDAO'

var Login = {};

/**
 * 判断登录的状态
 * @method _init
 * @param callback
 */
Login._init = function (callback) {
    console.log("进入login-nrngRegister");
    var nrngRegisterFlag = CommonUtil.getUrlParam("nrngregister") || "";
    if (nrngRegisterFlag === "1") {
        //判断nickname是否为空
        var appusercode = CommonUtil.getUrlParam("usercode") || "";
        var appnickname = CommonUtil.getUrlParam("nickname") || "";
        if (appusercode !== "") {
          localStorage.setItem(Constant.storage.USER_CODE, appusercode);
        }
        if (appnickname !== "") {
          localStorage.setItem(Constant.storage.NICKNAME, appnickname);
        }
    }

    if (_.isFunction(callback)) {
      callback();
    }
};

/**
 * 获取用户usercode的方法
 * @method initUsercode
 * @param callback
 */
Login.initUsercode = function (callback) {
  var self = this;
  //判断客户端本地是否有手机号
  var getmobile = CommonUtil.getUrlParam("getmobile");
  if (getmobile == 1) {
    //查询12000004接口获取usercode
    RemoteDAO.callUC12000004(function (data) {
      if (Number(data.error) === 1) {
        if (_.isFunction(callback)) {
          callback();
        }
      } else {
        console.log(data);
        LocalDAO.setUserCode(data.data.body[0].USER_CODE);
        self.initNickname(data.data.body[0].USER_CODE, callback);
      }
    });
  } else {
    if (_.isFunction(callback)) {
      callback();
    }
  }
};

/**
 * 查询客户是否是注册状态
 * @method initNickname
 * @param usercode
 * @param callback
 */
Login.initNickname = function (usercode, callback) {
  var self = this;
  //查询12000104接口获取nickname
  RemoteDAO.callUC12000104(usercode, function (str) {
    if (Number(str.error) === 0) {
      LocalDAO.setUserCode(str.data.body[0].USER_CODE);
      if (str.data.body[0].USER_HEAD != "") {
        LocalDAO.setUserHead(str.data.body[0].USER_HEAD);
      }

      LocalDAO.setNickname(str.data.body[0].NICKNAME);
      LocalDAO.setWalletTotal(str.data.body[0].WALLET_TOTAL);
      LocalDAO.setGoodpointCount(str.data.body[0].GOODPOINT_COUNT);
      LocalDAO.setSubscribeUserCount(str.data.body[0].SUBSCRIBE_USER_COUNT);

      // var medals = {};
      // medals.list = str.data.body[0].HONOR_LIST;
      //
      // LocalDAO.setMedalsOwned(medals);
      //添加自选股列表
      //self.initSelfStock();
    }
    if (_.isFunction(callback)) {
      callback();
    }
  });
};

/**
 * 查询客户的自选股列表
 * @method initSelfStock
 */
Login.initSelfStock = function () {
  //查询12000104接口获取nickname
  RemoteDAO.callSelfList(function (str) {
    LocalDAO.setSelfStock(str.data);
  });
};

export default Login;

